<template>
  <div class="app-container">
    <el-button v-if="checkPermission(permissionRoles.server_group_add)" type="primary" @click="handleAdd">添加分组</el-button>
    <el-table v-loading="listLoading" :data="list" class="mt-30" border :default-sort="{prop: 'sort', order: 'descending'}">
      <el-table-column label="分组ID" prop="id" align="center"></el-table-column>
      <el-table-column label="分组名称" prop="name" align="center"></el-table-column>
      <el-table-column label="备注" prop="remark" align="center"></el-table-column>
      <el-table-column label="排序" sortable prop="sort" align="center"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(permissionRoles.server_group_edit)" type="text" @click="handleEdit(scope)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="分组名称" prop="name">
          <el-input ref="name" v-model="dataForm.name" class="defaut-input" placeholder="填写分组名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" class="defaut-input" placeholder="填写备注" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dataForm.sort" :min="1" :max="10" label="排序"></el-input-number>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { deepClone } from '@/utils'
import checkPermission from '@/utils/permission'
import { getServerGroupList, handleServerGroup, deleteServerGroup } from '@/api/ops'

const defaultItem = {
  id: '',
  name: '',
  title: ''
}
export default {
  name: 'ServerGroup',
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }]
      },
      list: [],
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        edit: '编辑',
        new: '新增'
      }
    }
  },
  computed: {
  },
  created() {
    this.getServerGroupList()
  },
  methods: {
    checkPermission,
    async getServerGroupList() {
      this.listLoading = true
      const res = await getServerGroupList()
      this.listLoading = false
      this.list = res.data
    },
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.getServerGroupList()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.dataForm = deepClone(scope.row)
      this.getServerGroupList()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('你确定要删除此列吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteServerGroup(row.id)
          this.list.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // const isEdit = this.dialogType === 'edit'
          handleServerGroup(this.dataForm).then(response => {
            this.getServerGroupList()
          })
          this.dialogVisible = false
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table {
  font-size: 14px;
}
.el-table td, .el-table th {
  padding: 10px 0;
}
.el-table th>.cell {
  padding-left: 10px;
  padding-right: 10px;
}
</style>
